<template>
  <!-- 单位类型列表 -->
  <div class="app-container">
    <div class="form-container">
      <el-form :inline="true" label-width="80px" :model="filters" class="selectFrom">
        <el-form-item label="类型名称" prop="typeName">
          <el-input v-model="filters.typeName" size="small" placeholder="请输入类型名称" clearable />
        </el-form-item>
      </el-form>
    </div>
    <!-- isFxied -->
    <div class="btns-group">
      <el-button v-clickForbidden icon="el-icon-search" size="middle" @click="handleQuery">搜索</el-button>
      <el-button v-clickForbidden icon="el-icon-refresh" size="middle" @click="resetQuery">重置</el-button>
      <el-button v-hasPermi="['project:supperType:add']" icon="el-icon-plus" size="middle" :disabled="multipleSelection.length > 0 && multipleSelection[0].isFixed === '0'" @click="handleAdd">新增</el-button>
      <el-button v-hasPermi="['project:supperType:edit']" icon="el-icon-edit" size="middle" :disabled="single" @click="handleUpdate">修改</el-button>
      <el-button v-hasPermi="['project:supperType:remove']" icon="el-icon-delete" size="middle" :disabled="single" @click="handleDelete">删除</el-button>
      <el-button id="Detail" v-hasPermi="['project:supperType:detail']" icon="el-icon-zoom-in" size="middle" :disabled="isDetail" @click="handleDetail">详情</el-button>
    </div>
    <el-row>
      <el-col :span="24">
        <!-- 表格展示start -->
        <div ref="table">
          <el-table
            ref="multipleTable"
            v-loading="tableLoading"
            stripe
            class="table-list"
            :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
              (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
            :data="treeData"
            :row-style="rowClass"
            @selection-change="handleSelectionChange"
            @row-click="rowChecked"
          >
            <el-table-column type="selection" width="38" align="center" />
            <el-table-column prop="typeName" label="类型名称" min-width="180" header-align="center" align="left" />
            <el-table-column prop="typeNum" label="类型编号" min-width="120" align="center" />
            <el-table-column prop="orderNum" label="排序" min-width="120" align="center" />
            <el-table-column prop="remark" :show-overflow-tooltip="true" label="备注" min-width="180" align="center" />
          </el-table>
        </div>
        <!-- 表格展示end-->
      </el-col>
    </el-row>
    <!-- 单位类型新增弹框 -->
    <addDialog ref="addDialog" />
    <!-- 单位类型详情弹框 -->
    <detailDialog ref="detailDialog" />
  </div>
</template>

<script>
import { listOrganization, removeOrganization } from '@/api/superviseBaseInfo/typeManage/organizationType'
import addDialog from './components/addDialog'
import detailDialog from './components/detailDialog'
import { mapState } from 'vuex'
export default {
  name: 'OrganizationType',
  components: {
    detailDialog,
    addDialog
  },
  data() {
    return {
      tableLoading: false, // 表格加载
      highLevelSet: false, // 高级搜索
      // 筛选条件
      filters: {
        pageNum: 1,
        pageSize: 20,
        typeName: ''
      },
      /* 列表 start */
      // 分页总条数
      total: 0,
      // 非单个禁用
      single: true,
      isDetail: true,
      // 非多个禁用
      multiple: true,
      // 选中表格对话框的存储的值
      multipleSelection: [],
      // 高亮样式
      selectRow: [],
      // 表格数据
      treeData: []
      /* 列表 end */
    }
  },
  computed: {
    ...mapState({
      needTagsView: (state) => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  mounted() {
    // 获取列表数据
    this.getList()
  },
  methods: {
    // 获取列表数据
    async getList() {
      this.tableLoading = true
      try {
        const { data: { rows }} = await listOrganization(this.filters)
        this.total = rows.length || 0
        this.treeData = rows
        this.tableLoading = false
        // this.AddTaleData(this.treeData, this.total)
      } catch {
        this.tableLoading = false
      }
    },
    // list表单补空白行
    AddTaleData(arr, num) {
      const tableHeight = this.$refs.table.offsetHeight
      const row = Math.floor(
        (tableHeight - 36) / 36 // 表格：表头/行高：36px
      )
      for (let i = 0; i < (row - num); i++) {
        this.treeData.push({})
      }
    },
    /* 表单行选中事件 start */
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      // 将选中的数组赋给常量 selectData
      const selectData = this.multipleSelection
      // 调用表格的方法将所有已选的都取消
      this.$refs.multipleTable.clearSelection()
      // 判断选中的数组个数是不是等于 1
      if (selectData.length === 1) {
        // 循环判断有没有和当前行的相同的
        selectData.forEach((item) => {
          // 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        // 不等于1时
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(selection) {
      if (JSON.stringify(selection[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
      } else {
        this.multipleSelection = selection
        this.isDetail = selection.length !== 1
        this.single = selection.length !== 1 || this.multipleSelection[0].isFixed === '0'
        this.multiple = !selection.length
        this.selectRow = []
        if (selection.length > 0) {
          selection.forEach((item, index) => {
            this.selectRow.push(this.treeData.indexOf(item))
          })
        }
        // 如果选择的数组大于1
        if (selection.length > 1) {
          // 清空所有的选中
          this.$refs.multipleTable.clearSelection()
          // 将数组的最后一个选中
          this.$refs.multipleTable.toggleRowSelection(selection.pop())
        }
      }
    },
    /* 表单行选中事件 end */

    /* 按钮 start */
    // 查询
    handleQuery() {
      this.getList()
    },
    // 重置搜索条件
    resetQuery() {
      this.filters = {
        pageNum: 1,
        pageSize: 20,
        typeName: ''
      }
      this.getList()
    },
    // 新增
    handleAdd() {
      const multip = this.multipleSelection
      this.$refs.addDialog.handleAdd('新增单位类型', undefined, multip.length && multip[0])
    },
    // 编辑
    handleUpdate() {
      this.$refs.addDialog.handleAdd('修改单位类型', this.multipleSelection[0].id)
    },
    // 详情
    handleDetail() {
      this.$refs.detailDialog.handleAdd('单位类型详情', this.multipleSelection[0].id)
    },
    // 修改
    handleDelete() {
      const child = this.multipleSelection[0]
      this.$confirm('此操作将永久删除该单位类型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          this.tableLoading = true
          const { code, message } = await removeOrganization(child.id)
          this.tableLoading = false
          if (code === 200) {
            this.msgSuccess(message)
            // 重新加载表格数据
            this.getList()
          } else if (code === 202) {
            this.msgWarning(message)
          }
        })
        .catch(() => {})
    }
    /* 按钮 end */
  }
}
</script>

